package com.example.eurekazuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 1 on 2017/8/17.
 */
@Component
public class TokenFilter extends ZuulFilter{

    private static Logger log = LoggerFactory.getLogger(TokenFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

//        RequestContext requestContext = RequestContext.getCurrentContext();
//        HttpServletRequest servletRequest = requestContext.getRequest();
//        log.info("{} >>> {}",servletRequest.getMethod(), servletRequest.getRequestURL().toString());
//        Object accessToken = servletRequest.getParameter("token");
//        if (accessToken == null){
//            log.warn("accessToken is null");
//            requestContext.setSendZuulResponse(false);
//            requestContext.setResponseStatusCode(401);
//            try {
//                requestContext.getResponse().getWriter().write("token is empty");
//            }catch (Exception e){
//
//            }
//
//            return null;
//        }
//        log.info("ok");
        return null;
    }

}
